# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=cargo-generate
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.23.3
pkgrel=1
pkgdesc="Use pre-existing git repositories as templates (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/cargo-generate/cargo-generate"
msys2_references=(
  'archlinux: cargo-generate'
  'purl: pkg:cargo/cargo-generate'
)
license=('spdx:MIT OR Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-libgit2" "${MINGW_PACKAGE_PREFIX}-libssh2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-mdbook"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-cargo-edit")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('c12af31c60b7ea53e138e4028a23934873e1385b311f35b46413697bdfdc4e8a')

prepare() {
  cd "${_realname}-${pkgver}"

  # bump git2 crate
  cargo upgrade -p git2@0.20.0

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  export LIBSSH2_SYS_USE_PKG_CONFIG=1
  export LIBGIT2_NO_VENDOR=1
  export OPENSSL_NO_VENDOR=1
  cargo build --release --frozen

  cd guide
  mdbook build
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test --release --locked
}

package() {
  cd "${_realname}-${pkgver}"

  install -Dm755 "target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"
  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "guide/book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
  install -Dm644 "LICENSE-MIT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"
  install -Dm644 "LICENSE-APACHE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
}
